package cn.bdqn.layui.controller;

import cn.bdqn.layui.entity.*;
import cn.bdqn.layui.entity.vo.MenuRoleVO;
import cn.bdqn.layui.entity.vo.UserVO;
import cn.bdqn.layui.service.user.UserService;
import cn.bdqn.layui.service.menu.MenuService;
import cn.bdqn.layui.service.role.RoleService;
import cn.bdqn.layui.util.ResponseResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class RoleController {

    @Resource
    RoleService roleService;

    @Resource
    UserService userService;

    @Resource
    MenuService menuService;

    @RequestMapping("/userjue")
    @ResponseBody
    public String findRoleAll(Integer rid,Integer page,Integer limit) {
        rid = rid == null ? 0 : rid;
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        List<Role> roleAll = roleService.findRolePage(rid,(page-1)*limit,limit);
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "");
        object.put("data", roleAll);
        object.put("count", roleAll.size());
        return object.toJSONString();
    }


    /**
     * 添加用户和角色
     *
     * @param userVO
     * @return
     */
    @RequestMapping("/addroleuser")
    @ResponseBody
    public ResponseResult roleAll(UserVO userVO) {
        int i = 0;
        User user = userService.findByUserName(userVO.getUsername(), 0);
        if (user == null) {

        } else {

        }

        if (userVO.getRoles().length > 0) {
            i = userService.addUser(userVO);
        } else {

        }
        return new ResponseResult(200, "", i);
    }


    @RequestMapping("/reviewCheckBox")
    @ResponseBody
    public ResponseResult reviewCheckBox(Integer id) {
        List<Role> roleList = roleService.findByIdROle(id);
        return new ResponseResult(0, "", roleList);
    }

    @RequestMapping("/addrolemenu")
    @ResponseBody
    public ResponseResult addrolemenu(MenuRoleVO menuRoleVO) {
        int i = roleService.addRoleMenu(menuRoleVO);
        return new ResponseResult(200, "", i);
    }


    @RequestMapping("/updateRole")
    public String updateRole(Integer id, Model model) {
        //根据id查询角色
        Role role = roleService.findByName(null, id);
        //查询所有菜单
        List<Menu> menuList = menuService.findByIdToParentId(0);
        //根据角色id查询出所属的菜单
        List<Menu> newMenuList = menuService.findByRidMenu(id);
        //定义一个int集合 存放id
        List<Integer> integerList = new ArrayList<>();
        for (Menu menu : newMenuList) {
            //把遍历出来的菜单id添加到集合
            integerList.add(menu.getId());
        }
        model.addAttribute("role", role);
        model.addAttribute("menuList", menuList);
        model.addAttribute("integerList", integerList);
        return "/views/user/administrators/roleform2.html";
    }

    @RequestMapping("/updateMenuRole")
    @ResponseBody
    public ResponseResult updateMenuRole(MenuRoleVO menuRoleVO) {
        int i = roleService.updateRoleMenu(menuRoleVO);
        return new ResponseResult(200, "", i);
    }

    @RequestMapping("/delRole")
    @ResponseBody
    public ResponseResult delRole(Integer id) {
        int i = roleService.delRole(id);
        if (i > 0) {
            List<MenuRoleVO> roleIdMenu = roleService.findRoleIdMenu(id);
            for (MenuRoleVO menuRoleVO : roleIdMenu){
                roleService.delRoleMenu(menuRoleVO.getRid());
            }
        }
        return new ResponseResult(0,"");
    }

}
